﻿using PrimeEditor.Common;
using PrimeEditor.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace PrimeEditor.GameProject;

[DataContract]
public class ProjectTemplate
{
    [DataMember]
    public string ProjectType { get; set; }
    [DataMember]
    public string ProjectFile { get; set; }
    [DataMember]
    public List<string> Folders { get; set; }
    public byte[] Icon { get; set; }
    public byte[] Screenshot { get; set; }
    public string IconFilePath { get; set; }
    public string ProjectFilePath {  get; set; }
}
internal class NewProject : ViewModelBase
{
    private readonly string _templatePath = @"C:\Users\fengliulin\source\repos\GameEngineLearning\PrimeEditor\ProjectTemplate";
    private string _name = "NewProject";

    public string ProjectName
    {
        get
        {
            return _name;
        }
        set
        {
            if (_name != value)
            {
                _name = value;
                OnprepertyChanged(nameof(ProjectName));
            }
        }
    }


    private string _path = $@"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}\PrimalProject\";


    public string ProjectPath
    {
        get
        {
            return _path;
        }
        set
        {
            if (_path != value)
            {
                _path = value;
                OnprepertyChanged(nameof(ProjectPath));
            }
        }
    }

    private ObservableCollection<ProjectTemplate> _projectTemplates = new ObservableCollection<ProjectTemplate>();
    public ReadOnlyObservableCollection<ProjectTemplate> ProjectTemplates { get; }

    public NewProject()
    {
        ProjectTemplates = new ReadOnlyObservableCollection<ProjectTemplate>(_projectTemplates);
        try
        {
            var templateFiles = Directory.GetFiles(_templatePath, "template.xml", SearchOption.AllDirectories);
            Debug.WriteLine(templateFiles.Any());
            foreach (var file in templateFiles)
            {
                //var template = new ProjectTemplate()
                //{
                //    ProjectFile = "ProjectTemplate.primal",
                //    ProjectType = "Empty Project",
                //    Folders =new List<string>() { ".Primal", "Content", "GameCode"}
                //};

                //Serializer.ToFile(template, file);

                var template = Serializer.FromFile<ProjectTemplate>(file);
                template.IconFilePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(file), "Icon.png"));
                //template.Icon = File.ReadAllBytes(template.IconFilePath);
                template.IconFilePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(file), "Screenshot.png"));
                //template.Screenshot = File.ReadAllBytes(template.sc)
                template.ProjectFilePath= Path.GetFullPath(Path.Combine(Path.GetDirectoryName(file), template.ProjectFile));

                _projectTemplates.Add(template);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }


    }

}





